import uuid
from typing import List

from fastapi import Request
from pydantic import BaseModel
from schemas.response_entity import JsonModel
from db.repository.knowledge import db_get_kb_by_id, db_add_recommend_question, db_get_recommend_question, db_update_recommend_question, db_delete_recommend_question


class AddRecommendQuestionEntity(BaseModel):
    kb_id: uuid.UUID
    question: str


class UpdateRecommendQuestionEntity(BaseModel):
    recommend_question_id: uuid.UUID
    question: str


class DeleteRecommendQuestionEntity(BaseModel):
    recommend_question_id: uuid.UUID


async def recommend_questions(request: Request, kb_id: uuid.UUID):
    user = request.state.user

    kb = db_get_kb_by_id(kb_id)
    if not kb:
        return JsonModel(code=400, data=[], msg="知识库不存在").to_response()

    res: List = db_get_recommend_question(kb_id)
    return JsonModel(code=200, data=[i.to_dict() for i in res], msg="success").to_response()


async def add_recommend_question(request: Request, payload: AddRecommendQuestionEntity):
    user = request.state.user

    kb = db_get_kb_by_id(payload.kb_id)
    if not kb:
        return JsonModel(code=400, data=[], msg="知识库不存在").to_response()

    res = db_add_recommend_question(payload.kb_id, payload.question)
    if not res:
        return JsonModel(code=400, data=[], msg="添加推荐问题失败").to_response()
    return JsonModel(code=200, data=[res.to_dict()], msg="success").to_response()


async def update_recommend_question(request: Request, payload: UpdateRecommendQuestionEntity):
    user = request.state.user

    res = db_update_recommend_question(payload.recommend_question_id, payload.question)
    if not res:
        return JsonModel(code=400, data=[], msg="更新推荐问题失败").to_response()
    return JsonModel(code=200, data=[res.to_dict()], msg="success").to_response()


async def delete_recommend_question(request: Request, payload: DeleteRecommendQuestionEntity):
    user = request.state.user
    res = db_delete_recommend_question(payload.recommend_question_id)
    if not res:
        return JsonModel(code=400, data=[], msg="删除推荐问题失败").to_response()
    return JsonModel(code=200, data=[res.to_dict()], msg="success").to_response()
